package etcd

import (
	"context"
	clientv3 "go.etcd.io/etcd/client/v3"
	"time"
)

func Get(key string) (string, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	resp, err := etcdClient.Get(ctx, key)
	if err != nil {

		return "", err
	}
	return string(resp.Kvs[0].Value), nil
}

// Watch 对于监听key每个启动一个Goroutine 来去监听
func Watch(key string, watchHandle func(s clientv3.WatchChan)) {
	watch := etcdClient.Watch(context.Background(), key)
	go watchHandle(watch)
}
